﻿Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports Gadgeteer.Modules.GHIElectronics
Imports Gadgeteer.Modules.Edu.Ghi


Namespace GadgeteerApp1
    Partial Public Class Program

        ' This is run when the mainboard is powered up or reset. 
        Public Sub ProgramStarted()
            Const dot = 300  ' dot
            Const dash = 900  ' dash is three times as long as a dot
            Const rest = 900 ' rest between letters is three times as long as a dot
            Const gap = 300 ' gap between sounds is as long as one dot
            Const frequency = 300 ' chosen frequency

            Dim melody As Melody = New Melody()
            Debug.Print("Program Started")
            Dim newWord As String = ReadFromFile()

            For count As Integer = 0 To newWord.Length() - 1
                Debug.Print(newWord(count))

                Select Case newWord(count)
                    Case "A"  ' dot dash
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                    Case "B"  ' dash dot dot dot
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)

                    Case "C"  ' dash dot dash dot
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                    Case "D" '	-..
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                    Case "E" '	.
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                    Case "F" '	..-."
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                    Case "G" '	--.		
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                    Case "H" '	...."
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                    Case "I" '	..	
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                    Case "J" '	.---"
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                    Case "K" '	-.-	
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                    Case "L" '	.-.."
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                    Case "M" '	--		
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                    Case "N" '	-."
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                    Case "O" '	---		
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                    Case "P" '	.--."

                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                    Case "Q" '	--.-	
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                    Case "R" '	.-."

                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                    Case "S" '	...		
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                    Case "T" '	-"
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                    Case "U" '	..-		
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                    Case "V" '	...-"
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                    Case "W" '	.--		
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                    Case "X" '	-..-"
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                    Case "Y" '	-.--		
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                    Case "Z" '	--.."
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)
                        melody.Add(frequency, dash)
                        melody.Add(0, gap)

                        melody.Add(frequency, dot)
                        melody.Add(0, gap)
                        melody.Add(frequency, dot)
                        melody.Add(0, gap)

                End Select
                melody.Add(0, rest)

            Next
            tunes.Play(melody)


        End Sub

        Private Function ReadFromFile() As String
            Dim sdStorage As GT.StorageDevice = sdCard.GetStorageDevice()
            Dim dataFromFile As Byte()
            Dim newWord As String
            

            If sdCard.IsCardInserted Then
                dataFromFile = sdStorage.ReadFile("word.txt")
                newWord = System.Text.Encoding.UTF8.GetChars(dataFromFile)
            Else
                Debug.Print("There is a problem with your SD Card")
                Debug.Print("Make sure it is inserted correctly into the SD Card module")
            End If
            Return newWord
        End Function


        ' If you want to do something periodically, declare a GT.Timer by uncommenting the below line
        '   and then use the dropdown boxes at the top of this window to generate a Tick event handler.
        ' Dim WithEvents timer As GT.Timer = new GT.Timer(1000)  ' every second (1000ms)

    End Class
End Namespace